package com.jcark.arch.net

import android.annotation.SuppressLint
import android.app.Activity
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.telephony.TelephonyManager

object NetworkUtils {
    private var mConnectivityManager: ConnectivityManager? = null

    fun getNetString(context: Context): String {
        @SuppressLint("WrongConstant") val cm =
            context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        if (null != cm) {
            val activeNetworkInfo = cm.activeNetworkInfo
            if (null != activeNetworkInfo && activeNetworkInfo.state == NetworkInfo.State.CONNECTED) {
                if (activeNetworkInfo.type == ConnectivityManager.TYPE_MOBILE) {
                    return "WWAN"
                }
                if (activeNetworkInfo.type == ConnectivityManager.TYPE_WIFI) {
                    return "WiFi"
                }
            } else {
                return "Unknown"
            }
        }
        return "Unknown"
    }

    fun canConnect(context: Context): Boolean {
        //结果返回值
        var netType = false
        //获取手机所有连接管理对象
        val manager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        //获取NetworkInfo对象
        val networkInfo = manager.activeNetworkInfo ?: return netType
        //NetworkInfo对象为空 则代表没有网络
        //否则 NetworkInfo对象不为空 则获取该networkInfo的类型
        val nType = networkInfo.type
        if (nType == ConnectivityManager.TYPE_WIFI) {
            //WIFI
            netType = true
        } else if (nType == ConnectivityManager.TYPE_MOBILE) {
            val nSubType = networkInfo.subtype
            val telephonyManager =
                context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
            //3G   联通的3G为UMTS或HSDPA 电信的3G为EVDO
            netType = if (nSubType == TelephonyManager.NETWORK_TYPE_LTE
                && !telephonyManager.isNetworkRoaming
            ) {
                true
            } else if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS || nSubType == TelephonyManager.NETWORK_TYPE_HSDPA || (nSubType == TelephonyManager.NETWORK_TYPE_EVDO_0
                        && !telephonyManager.isNetworkRoaming)
            ) {
                true
                //2G 移动和联通的2G为GPRS或EGDE，电信的2G为CDMA
            } else if (nSubType == TelephonyManager.NETWORK_TYPE_GPRS || nSubType == TelephonyManager.NETWORK_TYPE_EDGE || (nSubType == TelephonyManager.NETWORK_TYPE_CDMA
                        && !telephonyManager.isNetworkRoaming)
            ) {
                true
            } else {
                true
            }
        }
        return netType
    }

    fun getAPNType(context: Context): String {
        //结果返回值
        var netType = "Unknown"
        //获取手机所有连接管理对象
        val manager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        //获取NetworkInfo对象
        val networkInfo = manager.activeNetworkInfo ?: return netType
        //NetworkInfo对象为空 则代表没有网络
        //否则 NetworkInfo对象不为空 则获取该networkInfo的类型
        val nType = networkInfo.type
        if (nType == ConnectivityManager.TYPE_WIFI) {
            //WIFI
            netType = "WiFi"
        } else if (nType == ConnectivityManager.TYPE_MOBILE) {
            val nSubType = networkInfo.subtype
            val telephonyManager =
                context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
            //3G   联通的3G为UMTS或HSDPA 电信的3G为EVDO
            netType = if (nSubType == TelephonyManager.NETWORK_TYPE_LTE
                && !telephonyManager.isNetworkRoaming
            ) {
                "WWAN"
            } else if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS || nSubType == TelephonyManager.NETWORK_TYPE_HSDPA || (nSubType == TelephonyManager.NETWORK_TYPE_EVDO_0
                        && !telephonyManager.isNetworkRoaming)
            ) {
                "WWAN"
                //2G 移动和联通的2G为GPRS或EGDE，电信的2G为CDMA
            } else if (nSubType == TelephonyManager.NETWORK_TYPE_GPRS || nSubType == TelephonyManager.NETWORK_TYPE_EDGE || (nSubType == TelephonyManager.NETWORK_TYPE_CDMA
                        && !telephonyManager.isNetworkRoaming)
            ) {
                "WWAN"
            } else {
                "WWAN"
            }
        }
        return netType
    }

    @SuppressLint("WrongConstant")
    private fun getConnectivityManager(cxt: Context): ConnectivityManager? {
        if (mConnectivityManager == null) {
            mConnectivityManager = cxt.getSystemService(
                Context.CONNECTIVITY_SERVICE
            ) as ConnectivityManager
        }
        return mConnectivityManager
    }

    /**
     * 获得网络连接是否可用
     *
     * @param context
     * @return
     */
    fun hasNetwork(context: Context): Boolean {
        @SuppressLint("WrongConstant") val con =
            context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val workInfo = con.activeNetworkInfo
        return !(workInfo == null || !workInfo.isAvailable)
    }

    /**
     * 判断是否是wifi连接
     */
    fun checkWifiState(context: Context): Boolean {
        var isWifiConnect = true
        @SuppressLint("WrongConstant") val cm =
            context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val networkInfos = cm.allNetworkInfo
        for (i in networkInfos.indices) {
            if (networkInfos[i].state == NetworkInfo.State.CONNECTED) {
                if (networkInfos[i].type == ConnectivityManager.TYPE_MOBILE) {
                    isWifiConnect = false
                }
                if (networkInfos[i].type == ConnectivityManager.TYPE_WIFI) {
                    isWifiConnect = true
                }
            }
        }
        return isWifiConnect
    }

    /**
     * 打开网络设置界面
     */
    fun openNet(activity: Activity) {
        val intent = Intent("/")
        val cm = ComponentName("com.android.settings", "com.android.settings.WirelessSettings")
        intent.component = cm
        intent.action = "android.intent.action.VIEW"
        activity.startActivityForResult(intent, 0)
    }

    /**
     * Check if there is an active network connection
     */
    fun isConnected(context: Context): Boolean {
        val connMgr = getConnectivityManager(context) ?: return false
        var network: NetworkInfo? = null
        try {
            network = connMgr.activeNetworkInfo
        } catch (e: Exception) {
            return false
        }
        return network != null && network.isConnected
    }

    fun isNetworkAvailable(context: Context): Boolean {
        val connMgr = getConnectivityManager(context) ?: return false
        var network: NetworkInfo? = null
        try {
            network = connMgr.activeNetworkInfo
        } catch (e: Exception) {
            return false
        }
        return network != null && network.isAvailable
    }
}